/*
 * 文件名: DetailView.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2012-1-16
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ViewAnimator;

import com.longevityassistant.activity.R;

/**
 * @author EX-XIAOFANQING001
 * @date 2012-1-16
 * @version [Android PABank C01, @2012-1-16]
 * @description
 */
public class DetailView extends FrameLayout
{
    
    /**
     * 查看上一条记录
     */
    private ImageButton btnViewPre;
    
    /**
     * 查看下一条记录
     */
    private ImageButton btnViewNext;
    
    /**
     * 动画切换器
     */
    private ViewAnimator vaTransactionDetailContainer;
    
    /**
     * 当前查看的交易详情列表
     */
    private List<Map<String, Object>> mCurrentList;
    
    /**
     * 当前显示的详情页面
     */
    private int currentDisplayIndex;
    
    /**
     * 保存VIEW
     */
    private View[] views = new View[2];
    /**
     * view索引
     */
    private int viewIndex = 0;
    
    private View nextView;
    
    private boolean isInAnimater;
    
    
    public DetailView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    public DetailView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public DetailView(Context context)
    {
        super(context);
        init();
    }

    private void init() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.app_widget_layout_detail, null);
        addView(root, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        btnViewPre = (ImageButton) findViewById(R.id.ibtn_view_pre);
        btnViewNext = (ImageButton) findViewById(R.id.ibtn_view_next);
        vaTransactionDetailContainer = (ViewAnimator) findViewById(R.id.va_transation_detail_container);
        
        btnViewPre.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (isInAnimater) {
                    return ;
                }
                
                // 显示上一条记录的详情
                
                try{
                    int toIndex = currentDisplayIndex-1;
                    if(toIndex>=0&&toIndex<mCurrentList.size() && createDetailView != null){
                        nextView = createDetailView.createDetailView(mCurrentList.get(toIndex), views[viewIndex], toIndex);
                        if(views[viewIndex]!=null){
                        	vaTransactionDetailContainer.removeView(views[viewIndex]);
                        }
                        currentDisplayIndex = toIndex;
                        vaTransactionDetailContainer.addView(nextView);
                        
                        Animation pushRightIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_in);
                        Animation pushRightOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_out);
                        pushRightIn.setDuration(600);
                        pushRightIn.setAnimationListener(animationListener);              
                        pushRightOut.setDuration(600);
                        vaTransactionDetailContainer.setInAnimation(pushRightIn);
                        vaTransactionDetailContainer.setOutAnimation(pushRightOut);
                        vaTransactionDetailContainer.showNext();
                    }
                   
                }catch(Exception ex){
                    // ignore 
                }
            }
        });
        
        btnViewNext.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (isInAnimater) {
                    return ;
                }
                
                try{
                     int toIndex = currentDisplayIndex+1;
                     if(toIndex>=0&&toIndex<mCurrentList.size() && createDetailView != null){
                         nextView = createDetailView.createDetailView(mCurrentList.get(toIndex), views[viewIndex], toIndex);
                         if(views[viewIndex]!=null){
                         	
                         	vaTransactionDetailContainer.removeView(views[viewIndex]);
                         }
                         currentDisplayIndex =toIndex;
                         vaTransactionDetailContainer.addView(nextView);
                         
                         Animation pushLeftIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in);
                         Animation pushLeftOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out);
                         pushLeftIn.setAnimationListener(animationListener);
                         pushLeftIn.setDuration(600);
                         pushLeftOut.setDuration(600);
                         vaTransactionDetailContainer.setInAnimation(pushLeftIn);
                         vaTransactionDetailContainer.setOutAnimation(pushLeftOut);
                         vaTransactionDetailContainer.showNext();
                     }
                }catch(Exception ex){
                    //ignore 
                }
            }
        });
    }
    
  
    /**
     * 滑动动画结束时判定左右控制按钮的显示情况
     */
    private AnimationListener animationListener = new AnimationListener()
    {
        
        @Override
        public void onAnimationStart(Animation animation)
        {
            isInAnimater = true;
        }
        
        @Override
        public void onAnimationRepeat(Animation animation)
        {
            // do nothing
        }
        
        @Override
        public void onAnimationEnd(Animation animation)
        {
            isInAnimater = false;
            vaTransactionDetailContainer.setInAnimation(null);
            vaTransactionDetailContainer.setOutAnimation(null);
            vaTransactionDetailContainer.removeViewAt(0);
            showControlBtn();
        }
    };
    
    public void setDetailData(List<Map<String, Object>> datas, int currentPos, CreateDetailView cdv) {
        try {
            createDetailView = cdv;
            mCurrentList = datas;
            currentDisplayIndex = currentPos;
            
            nextView = cdv.createDetailView(mCurrentList.get(currentDisplayIndex), null, currentPos);
            vaTransactionDetailContainer.addView(nextView);
            
            showControlBtn();
        } catch (Exception e) {
            // ignore
        }
    }
    
    /**
     * 控制是否显示左右切换的按钮
     * [一句话功能简述]<BR>
     */
    private void showControlBtn() {
        if (views[viewIndex] == null) {
    		views[viewIndex] = nextView;
    	}
        viewIndex = Math.abs(viewIndex-1);  
        if (currentDisplayIndex == 0) {
            btnViewPre.setVisibility(View.GONE);
        } else {
            btnViewPre.setVisibility(View.VISIBLE);
        }
        
        if (currentDisplayIndex == mCurrentList.size() - 1) {
            btnViewNext.setVisibility(View.GONE);
        } else {
            btnViewNext.setVisibility(View.VISIBLE);
        }
    }
    
    public void setCreateDetailView(CreateDetailView createDetailView)
    {
        this.createDetailView = createDetailView;
    }

    public CreateDetailView getCreateDetailView()
    {
        return createDetailView;
    }

    private CreateDetailView createDetailView; 
    
    public interface CreateDetailView {
        public View createDetailView(Map<String, Object> detailDatas, View view, int currentPos);
    }
    
    public int getCurrentDisplayPosition() {
        return currentDisplayIndex;
    }
}

